package com.bzr.emos.wx.Service;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bzr.emos.wx.db.dao.BzrActivationCodeDao;
import com.bzr.emos.wx.db.dao.TbUserDao;
import com.bzr.emos.wx.db.dao.UserDao;
import com.bzr.emos.wx.db.dao.WechatUsersDao;
import com.bzr.emos.wx.db.pojo.BzrActivationCode;
import com.bzr.emos.wx.db.pojo.TbUser;
import com.bzr.emos.wx.db.pojo.User;
import com.bzr.emos.wx.db.pojo.WechatUsers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;

@Service
@Slf4j
@Scope("prototype")
public class UserService {

    @Autowired
    private UserDao userDao;

    @Value("${wx.app-secret}")
    private String appSecret;

    @Value("${wx.app-id}")
    private String appId;


    @Autowired
    TbUserDao tbUserDao;

    @Autowired
    BzrActivationCodeDao bzrActivationCodeDao;

    @Autowired
    WechatUsersDao wechatUsersDao;


    //创建用户
    public int addUserDetails(User user) {

        if (user.getPassword() != null && user.getPassword().length() > 2) {
            User username = userDao.selectOne(new QueryWrapper<>(user).eq("username", user.getUsername()));
            if (username != null) {
                return 0;
            } else {
                PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(4);
                String password = passwordEncoder.encode(user.getPassword());
                user.setPassword(password);
                return userDao.insert(user);
            }
        }
        return -1;
    }

    //获取微信授权信息
    public JSONObject getOpenId(String code) {
        JSONObject retJson=new JSONObject();
        String url = "https://api.weixin.qq.com/sns/jscode2session"
                + "?appid=" + appId +
                "&secret=" + appSecret +
                "&js_code=" + code +
                "&grant_type=authorization_code";
        String ret = HttpUtil.get(url);
        if (ret != null) {
             retJson = JSONObject.parseObject(ret);
        }
        return retJson;
    }


    //查询是否存在root用户
    public boolean checkOpenId() {
        TbUser tbUser = tbUserDao.selectOne(new QueryWrapper<>(new TbUser()).eq("role", 0));
        if (tbUser != null) {
            return true;
        } else {
            return false;
        }
    }


    //验证激活码是否存在
    public boolean checkPassword(String code) {
        log.info("验证激活码是否存在:{}",code);
        BzrActivationCode bzrActivationCode = bzrActivationCodeDao.selectOne(new QueryWrapper<>(new BzrActivationCode())
                .eq("is_disabled", 0)
                .eq("code", code));
        log.info("返回信息:{}",bzrActivationCode);
        if (bzrActivationCode != null) {
            bzrActivationCode.setUseActivation(bzrActivationCode.getUseActivation()+1);
            int i = bzrActivationCodeDao.updateById(bzrActivationCode);
            log.info("更新状态:{}",i);
            return true;
        }


        return false;
    }







    //保存微信用户
    public boolean addWechatUser(WechatUsers wechatUsers) {
        int insert = wechatUsersDao.insert(wechatUsers);
        log.info("保存微信用户返回值:{}",insert);
        return insert==1?true:false;
    }

    //判断数据库是否存在此微信用户openid
    public WechatUsers checkWechatUser(String code) {

        WechatUsers wtuser = wechatUsersDao.selectOne(new QueryWrapper<>(new WechatUsers()).eq("openid", code));


        return wtuser;
    }



}
